﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using pointType = TutorialPointer.pointType;


class FingerMove : MonoBehaviour
{
    public Vector2 startPos;
    public Vector2 endPos;
    FingerMoveNode.moveInfo info;
    public float startWaitTime;
    public float moveTime;
    public float startTime = 0;
    public pointType startType;
    public bool ispingpong;

    public pointType nowType
    {
        get
        {
            return _nowType;
        }
        set
        {
            _nowType = value;
            SetActiveForPointType();
        }
    }

    public pointType _nowType;

    public float tick = 0;

    public Image normal;
    public Image hold;
    public Transform pointerRoot;
    public Vector2 imgeSize;
    public void StartMove(FingerMoveNode.moveInfo info, float startwaitTime, float moveTime, pointType startType, bool ispingpong)
    {
        this.gameObject.SetActive(true);
        this.pointerRoot.gameObject.SetActive(true);
        this.info = info;
        this.startWaitTime = startwaitTime;
        this.moveTime = moveTime;
        this.startType = startType;
        this.ispingpong = ispingpong;
        this.imgeSize = (this.normal.transform as RectTransform).sizeDelta / 2;
        StartCoroutine(StartInit());

    }

    private IEnumerator StartInit()
    {
        yield return new WaitForEndOfFrame();
        StartCoroutine(SetAsLastSibling());
        InitPostion();
        ResetMove();
    }

    private IEnumerator SetAsLastSibling()
    {
        yield return new WaitForSeconds(2);
        transform.SetAsLastSibling();
    }

    public void Stop()
    {
        gameObject.SetActive(false);
    }

    public void InitPostion()
    {
        switch (info.startPosType)
        {
            case ElementType.ScreenPercent:
                this.startPos = TutorialUIManager.Instance.GetPositionWithPercent(info.start_SceenPercent);
                break;
            case ElementType.areaIndex:
                this.startPos = TutorialLogicUtils.GetUIPosByAreaIndex(info.start_areaIndex);
                break;
            case ElementType.buildingID:
                this.startPos = TutorialLogicUtils.GetBuildingUIPos(info.start_BuildingID);
                break;
            case ElementType.uiPath:
                this.startPos = GetPosWithUIPath(info.start_uiPath);
                break;
            default:
                break;
        }
        this.startPos.x -= imgeSize.x / 2;
        this.startPos.y += imgeSize.y / 2;
        switch (info.endPosType)
        {
            case ElementType.ScreenPercent:
                this.endPos = TutorialUIManager.Instance.GetPositionWithPercent(info.end_SceenPercent);
                break;
            case ElementType.areaIndex:
                this.endPos = TutorialLogicUtils.GetUIPosByAreaIndex(info.end_areaIndex);
                break;
            case ElementType.buildingID:
                this.endPos = TutorialLogicUtils.GetBuildingUIPos(info.end_BuildingID);
                break;
            case ElementType.uiPath:
                this.endPos = GetPosWithUIPath(info.end_uiPath);
                break;
            default:
                break;
        }
        this.endPos.x -= imgeSize.x / 2;
        this.endPos.y += imgeSize.y / 2;
    }

    protected Vector2 GetPosWithUIPath(List<string> uiPath)
    {
        Vector2 pos;
        var image = TutorialLogicUtils.GetTransfromByUIPath(Hope.UIManager.Instance.UIRoot, uiPath);
        pos = image.TransformToCanvasLocalPosition(Hope.UIManager.Instance.UIRoot.GetComponent<Canvas>());
        return pos;
    }

    public void ResetMove()
    {
        pointerRoot.localPosition = startPos;
        tick = 0;
        this.nowType = startType;
    }

    public bool CheckCanMove()
    {
        tick += Time.deltaTime;
        if (tick >= startWaitTime)
        {
            return true;
        }
        return false;
    }
    /// <summary>
    /// 获取手指信息
    /// </summary>
    public void Update()
    {
        if (CheckCanMove())
        {
            if (Vector3.Distance(pointerRoot.localPosition, endPos) <= 0.001)
            {
                ResetMove();
            }
            pointerRoot.localPosition = Vector3.MoveTowards(pointerRoot.localPosition, endPos, 5);
        }
        else
        {
            if (tick >= startWaitTime / 2 && this.nowType == startType)
            {
                ChangeState();
            }
        }


        // 按下
        if (InputUtils.GetMouseButtonDown(0))
        {
            this.pointerRoot.gameObject.SetActive(false);
        }

        //松手
        if (InputUtils.GetMouseButtonUp(0))
        {
            ResetMove();
            this.pointerRoot.gameObject.SetActive(true);
        }

    }

    public void ChangeState()
    {
        if (nowType == pointType.hold)
        {
            nowType = pointType.normal;
        }
        else if (nowType == pointType.normal)
        {
            nowType = pointType.hold;
        }
    }

    public void SetActiveForPointType()
    {
        if (nowType == pointType.hold)
        {
            normal.gameObject.SetActive(false);
            hold.gameObject.SetActive(true);
        }
        else if (nowType == pointType.normal)
        {
            normal.gameObject.SetActive(true);
            hold.gameObject.SetActive(false);
        }
    }
}
